using AutoMapper;
using B.S.Medical.Api.Read.Application.Command.Doctor;
using B.S.Medical.Api.Read.DTOS;
using B.S.Medical.Domain.Doctor;
using B.S.Medical.ErrorCode;
using B.S.Medical.Infrastructure.Interface;
using MediatR;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.Medical.Api.Read.Application.Handler.Doctor
{
    /// <summary>
    /// 获取院区列表处理器
    /// </summary>
    public class GetCampusHandler : IRequestHandler<GetCampusCommand, ApiResult<List<CampusDTO>>>
    {
        private readonly IBaseRepository<CampusModel> _campusRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetCampusHandler> _logger;

        public GetCampusHandler(IBaseRepository<CampusModel> campusRepository, IMapper mapper, ILogger<GetCampusHandler> logger)
        {
            _campusRepository = campusRepository;
            _mapper = mapper;
            _logger = logger;
        }

        public async Task<ApiResult<List<CampusDTO>>> Handle(GetCampusCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<List<CampusDTO>>();
            try
            {
                var list = _campusRepository.GetAll().Where(c => !c.IsDeleted);
                res.Data = _mapper.Map<List<CampusDTO>>(list.ToList());
                res.Code = ApiEnum.Success;
                res.Msg = "获取院区列表成功";
            }
            catch (System.Exception ex)
            {
                res.Data = null;
                res.Code = ApiEnum.Fail;
                res.Msg = "获取院区列表异常: " + ex.Message;
                _logger.LogError(ex, "获取院区列表异常");
            }
            return res;
        }
    }
} 